package com.xxx.controller;

import com.xxx.entity.Category;
import com.xxx.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("/admin/category")
@Controller
public class AdminCategoryController {

    @Autowired
    private CategoryService categoryService;


    @RequestMapping("/list")
    public String list(Model model){
        List<Category> categoryList = categoryService.list();
        model.addAttribute("categoryList",categoryList);
        return "/admin/category_list";
    }


    @RequestMapping("/add")
    public String add(){
        return "/admin/category_add";
    }


    @RequestMapping("/add_submit")
    public String add_submit(Category category){
        categoryService.add(category);
        return "redirect:/admin/category/list";
    }


    @RequestMapping("/update_show")
    public String update(Integer categoryId,Model model){
        // 根据id查询商品
        Category category = categoryService.findById(categoryId);
        model.addAttribute("category",category);
        return "/admin/category_update";
    }


    @RequestMapping("/update_submit")
    public String update_submit(Category category){
        categoryService.update(category);
        return "redirect:/admin/category/list";
    }
}
